package com.jishuo.pzk.service;

import com.google.gson.Gson;
import com.jishuo.base.config.PzkConfig;
import com.jishuo.pzk.bean.base.VouFieldVO;
import com.jishuo.pzk.bean.base.VoucherDetailVO;
import com.jishuo.pzk.bean.base.VoucherVO;
import com.jishuo.pzk.bean.bill.BackPayBillVO;
import com.jishuo.pzk.util.IdWorker;
import com.jishuo.pzk.util.ImageUtils;
import com.jishuo.pzk.util.NumberToCn;
import com.jishuo.pzk.util.VoucherUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 退付
 */
@Service
public class BackBillService {
    private static final String vocde = PzkConfig.Vocode_BackBill;
    private static final int busitype = PzkConfig.Pztype_BackBill;//凭证类型

    @Resource
    private VoucherService voucherService;

    @Resource
    private VoucherUtil voucherUtil;

    /**
     * 存储凭证数据
     * @param data 报文
     * @return
     */
    public int savePzk(String data) {
        BackPayBillVO vo = new Gson().fromJson(data, BackPayBillVO.class);
        VoucherVO voucher = voucherUtil.getVoucher(busitype, "0", vocde, vo.getBackBillNum(), "0", "2", vo.getCantonId(), vo.getBankId(), "0", vo.getBankStation(), vo.getDealDate(), new Gson().toJson(vo));
        return voucherService.saveVoucher(voucher);
    }

    /**
     * 获得凭证字段明细
     * @param list 字段定义
     */
    public static List<VoucherDetailVO> getDetail(List<VouFieldVO> list, VoucherVO voucher) {
        String VO_SN = voucher.getVoSn();
        String ORDERBILLSN = voucher.getOrderbillsn();
        BackPayBillVO vo = new Gson().fromJson(voucher.getVoucher(), BackPayBillVO.class);
        List<VoucherDetailVO> listDe = new ArrayList<>();

        for (VouFieldVO vouFieldVO : list) {
            int x;
            int y;
            //int x1;
            String pos = "";
            String pos1 = "";

            if ("left".equals(vouFieldVO.getHalign()) || vouFieldVO.getHalign() == null) {
                pos = vouFieldVO.getFieldPosLeft();

            } else if ("right".equals(vouFieldVO.getHalign())) {
                pos = vouFieldVO.getFieldPosRight();

            } else if ("center".equals(vouFieldVO.getHalign())) {
                pos = vouFieldVO.getFieldPosLeft();
                pos1 = vouFieldVO.getFieldPosRight();
                // x1=Integer.parseInt(pos1.split(",")[0]);
            }
            if (vouFieldVO.getField() != null) {
                if (vouFieldVO.getField().trim().equals("fillday")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("fillday");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDealDate().substring(8, 10));
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("fillyear")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("fillyear");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDealDate().substring(0, 4));
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("fillmonth")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("fillmonth");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDealDate().substring(5, 7));
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }


                if (vouFieldVO.getField().trim().equals("no")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("billnum");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getBackBillNum());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }

                if (vouFieldVO.getField().trim().equals("payeename")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("payeename");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDrawMan());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("payeeaccount")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);

                    voucherDetailVO.setField("payeeaccount");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDrawAccount());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("payeebank")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("payeebank");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getDrawBank());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("bankaccountname")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bankaccountname");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getAccountName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("bankaccountcode")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bankaccountcode");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getAccountCode());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("bankname")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bankname");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getBankName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }

                if (vouFieldVO.getField().trim().equals("itemcode")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("itemcode");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getItemCode());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }

                if (vouFieldVO.getField().trim().equals("itemname")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("itemname");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getItemName());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("memo")) {

                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("memo");
                    voucherDetailVO.setFieldType("String");
                    // 退付书退付原因置空
                    voucherDetailVO.setFieldValue("");
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);

                }
                if (vouFieldVO.getField().trim().equals("bigamt")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    BigDecimal numberOfMoney = new BigDecimal(vo.getFund());
                    String bigamt = NumberToCn.number2CNMontrayUnit(numberOfMoney);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");

                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("bigamt");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(bigamt);
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
                if (vouFieldVO.getField().trim().equals("amt")) {
                    VoucherDetailVO voucherDetailVO = new VoucherDetailVO();
                    voucherDetailVO.setVoVdSn(IdWorker.getOrderCode(busitype));
                    voucherDetailVO.setVoDeSn(VO_SN);
                    voucherDetailVO.setOrderbillsn(ORDERBILLSN);

                    Color color = Color.BLACK;
                    String cc = ImageUtils.Color2String(color);
                    x = Integer.parseInt(pos.split(",")[0]);
                    y = Integer.parseInt(pos.split(",")[1]);

                    voucherDetailVO.setAlpha(1);
                    voucherDetailVO.setFontcolor(cc);
                    voucherDetailVO.setFontname("宋体");
                    voucherDetailVO.setFontsize(40);
                    voucherDetailVO.setHalign(vouFieldVO.getHalign());
                    voucherDetailVO.setX(x);
                    voucherDetailVO.setY(y);
                    voucherDetailVO.setField("amt");
                    voucherDetailVO.setFieldType("String");
                    voucherDetailVO.setFieldValue(vo.getFund());
                    voucherDetailVO.setMaxlength(vouFieldVO.getMaxlength());
                    voucherDetailVO.setIsnewline(vouFieldVO.getIsnewline());
                    listDe.add(voucherDetailVO);
                }
            }
        }
        return listDe;
    }
}
